layui.define(function(exports){
    var layer = layui.layer;
    var $ = layui.$;
    var obj = {
        //获取复选框选中值
        getMuiltCheckedValue:function(id){
            var chkIds = "";
            $("input[name='" + id + "']:checkbox").each(function () {
                if ($(this).prop("checked") == true) {
                    chkIds = chkIds + $(this).val() + ",";
                }
            });
            return chkIds;
        },
        //获取单选框选中值
        getSingleCheckedValue:function(id){
            var chkIds = $("input[name='" + id + "']:checked").val();
            return chkIds;
        },
        //验证是否为空
        isEmpty:function(str){
            if (str == null) {
                return true;
            }
            if (str == undefined) {
                return true;
            }
            if ($.trim(str) == "") {
                return true;
            }
            return false;
        },
        //随机生成数字
        createRandomNum:function(x,y){
            var randomNum = parseInt(Math.random() * (x - y + 1) + y);
            return randomNum;
        },
        //随机生成字母+数字字符串
        genRandomString:function(len){
            len = len || 32;
            var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'; // 默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1
            var maxPos = $chars.length;
            var pwd = '';
            for (i = 0; i < len; i++) {
                pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
            }
            return pwd;
        },
        //替换字符串
        replaceAll:function(str, sptr, sptr1){
            if(this.isEmpty(str)){
                return str;
            }
            while (str.indexOf(sptr) >= 0) {
                str = str.replace(sptr, sptr1);
            }
            return str;
        },
        //参数驼峰转下换线
        covertHump:function(str){
            return str.replace(/([A-Z])/g, function(match) {
                return '_' + match.toLowerCase();
            });
        },
        //
        getFormObject:function(formId, prefix){
            var that = this;
            var arry = $("#" + formId).serializeArray();
            var model = {};
            $.each(arry, function () {
                if (!that.isEmpty(prefix)) {
                    //如果元素有分隔符号，如日期区域，则特殊处理
                    var name = that.replaceAll(this.name, prefix, "");
                    if(name.split("_").length>1){
                       for(var i=0;i<name.split("_").length;i++){
                           model[name.split("_")[i]] = $.trim(this.value.split("~")[i]);
                       }
                    }else{
                        model[name] = $.trim(this.value);
                    }
                } else {
                    var name = that.replaceAll(this.name, prefix, "");
                    console.info("name.size:"+name.split("_").length>1);
                    model[this.name] = $.trim(this.value);
                }
            });
            return model;
        },
        //清空form表单
        clearForm:function(formId){
            $("#" + formId + " input").each(function () {
                if($(this).attr("type").toLowerCase()=="text" || $(this).attr("type").toLowerCase()=="number"){
                    $(this).val('');
                }
            });
            $("#" + formId + " textarea").each(function () {
                $(this).val('');
            });
            $("#" + formId + " select").each(function () {
                $(this).val('');
            });
        },
        formatDtm:function(dtm){
            if(this.isEmpty(dtm)){
                return "";
            }else{
                var year = dtm.substr(0, 4);
                var month = dtm.substr(4, 2);
                var day = dtm.substr(6, 2);
                var hour = dtm.substr(8, 2);
                var min = dtm.substr(10, 2);
                var sec = dtm.substr(12, 2);
                return year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec;
            }
        },
        getTimeStamp:function(format){
            var date = new Date();
            var month = date.getMonth() + 1;
            var strDate = date.getDate();
            var hours = date.getHours()
            var minutes = date.getMinutes();
            var seconds = date.getMinutes();
            if (month >= 1 && month <= 9) {
                month = "0" + month;
            }
            if (strDate >= 0 && strDate <= 9) {
                strDate = "0" + strDate;
            }
            if (hours >= 0 && hours <= 9) {
                hours = "0" + hours;
            }
            if (minutes >= 0 && minutes <= 9) {
                minutes = "0" + minutes;
            }
            if (seconds >= 0 && seconds <= 9) {
                seconds = "0" + seconds;
            }
            var split = "";
            if (!this.isEmpty(format)) {
                split = format;
            }
            var currentdate = date.getFullYear() + split + month + split + strDate + split
                + hours + split + minutes + split + seconds;
            return currentdate;
        },
        formatDt:function(dt){
            if(this.isEmpty(dt)){
                return "";
            }else{
                var year = dt.substr(0, 4);
                var month = dt.substr(4, 2);
                var day = dt.substr(6, 2);
                return year + "-" + month + "-" + day;
            }
        },
        parseDateString:function(dateStr){
            // 确保日期字符串长度为14
            if(this.isEmpty(dateStr)){
                return '';
            }
            if (dateStr.length !== 14) {
                console.info(dateStr);
                throw new Error("日期格式应为 YYYYMMDDHHMISS");
            }
            var year = dateStr.substring(0, 4);
            var month = parseInt(dateStr.substring(4, 6)) - 1; // 月份从0开始
            var day = dateStr.substring(6, 8);
            var hour = dateStr.substring(8, 10);
            var minute = dateStr.substring(10, 12);
            var second = dateStr.substring(12, 14);
            return new Date(year, month, day, hour, minute, second);
        },
        diffDt:function(d1,d2){
            if(this.isEmpty(d1)){
                return 0;
            }
            if(this.isEmpty(d2)){
                d2 = this.getTimeStamp();
                console.info("d2:"+d2);
            }
            var date1 = this.parseDateString(d1);
            var date2 = this.parseDateString(d2);
            var diffTime = Math.abs(date2 - date1);
            var diffDays = Math.floor(diffTime / (1000 * 60 * 60 * 24));
            return diffDays;
        }
    };
    //输出模块
    exports('pub',obj);
});